package com.su.pg.data;

import com.amap.api.maps2d.model.LatLng;

import java.util.List;

/**
 * Created by Choco on 2015/6/1.
 */
public class LatLngUtil {
    private static final float PIXEL_THRESHHOLD = 50;
    private static double EARTH_RADIUS = 6378.137;//地球半径

    public static boolean isSameLatLng(LatLng a, LatLng b, float scalePerPixel) {
        boolean isSame = false;
        double distance = getDistance(a.latitude, a.longitude, b.latitude, b.longitude);

        double pixel = distance / scalePerPixel;

        if (pixel < PIXEL_THRESHHOLD) {
            isSame = true;
        }

        System.out.println("distance = " + distance + ", scalePerPixel = " + scalePerPixel);

        return isSame;
    }

    //  http://alienryderflex.com/polygon/
    //  Globals which should be set before calling this function:
    //
    //  int    polyCorners  =  how many corners the polygon has (no repeats)
    //  float  polyX[]      =  horizontal coordinates of corners
    //  float  polyY[]      =  vertical coordinates of corners
    //  float  x, y         =  point to be tested
    //
    //  (Globals are used in this example for purposes of speed.  Change as
    //  desired.)
    //
    //  The function will return YES if the point x,y is inside the polygon, or
    //  NO if it is not.  If the point is exactly on the edge of the polygon,
    //  then the function may return YES or NO.
    //
    //  Note that division by zero is avoided because the division is protected
    //  by the "if" clause which surrounds it.
    //
    //    bool pointInPolygon() {
    //
    //        int   i, j=polyCorners-1 ;
    //        bool  oddNodes=NO      ;
    //
    //        for (i=0; i<polyCorners; i++) {
    //            if ((polyY[i]< y && polyY[j]>=y
    //                    ||   polyY[j]< y && polyY[i]>=y)
    //                    &&  (polyX[i]<=x || polyX[j]<=x)) {
    //                if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
    //                    oddNodes=!oddNodes; }}
    //            j=i; }
    //
    public static boolean pointInPolygon(List<LatLng> polygonPoints, LatLng point) {
        int polyCorners = polygonPoints.size();
        int i = polyCorners - 1;
        int j = polyCorners - 1;
        boolean oddNodes = false;

        for (i = 0; i < polyCorners; i++) {
            if ((polygonPoints.get(i).longitude < point.longitude && polygonPoints.get(j).longitude >= point.longitude
                    || polygonPoints.get(j).longitude < point.longitude && polygonPoints.get(i).longitude >= point.longitude)
                    && (polygonPoints.get(i).latitude <= point.latitude
                    || polygonPoints.get(j).latitude <= point.latitude)) {
                if (polygonPoints.get(i).latitude +
                        (point.longitude - polygonPoints.get(i).longitude) /
                                (polygonPoints.get(j).longitude - polygonPoints.get(i).longitude) *
                                (polygonPoints.get(j).latitude - polygonPoints.get(i).latitude)
                        < point.latitude) {
                    oddNodes = !oddNodes;
                }
            }
            j = i;
        }

        return oddNodes;
    }


    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = s * 1000;
        return s;
    }
}
